package com.example.framework509.codeGen.utils;

import com.example.framework509.common.exceptions.JdbcException;
import com.example.framework509.common.exceptions.NotExistException;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Component
public class AnnotationUtil {

    public List<String> convertAnnotation(String annotationCode, Boolean nameContainMapper) throws Exception {
        List<String> classes = new ArrayList<>();
        List<Integer> codes = new ArrayList<>();
        for (int i = 0; i < annotationCode.length(); i++) {
            char c = annotationCode.charAt(i);
            int indexCode = Integer.parseInt(String.valueOf(c));
            if (indexCode != 1 && indexCode != 0) throw new JdbcException("注解编码异常");
            codes.add(indexCode);
        }
        Collections.reverse(codes);
        if (codes.size() != 4) throw new JdbcException("注解编码位数异常");
        if (codes.get(3) == 1) classes.add("EditKey");
        if (codes.get(2) == 1) classes.add("AllowNull");
        if (codes.get(1) == 1) classes.add("VerifyRepetitionField");
        if (codes.get(0) == 1) classes.add("VerifyMultiRepetitionField");
        return classes;
    }

//    public List<String> convertAnnotation(String annotationCode, Boolean nameContainMapper) throws Exception {
//        List<String> classes = new ArrayList<>();
//        List<Integer> codes = new ArrayList<>();
//        for (int i = 0; i < annotationCode.length(); i++) {
//            char c = annotationCode.charAt(i);
//            int indexCode = Integer.parseInt(String.valueOf(c));
//            if (indexCode != 1 && indexCode != 0) throw new JdbcException("注解编码异常");
//            codes.add(indexCode);
//        }
//        Collections.reverse(codes);
//        if (!nameContainMapper) {
//            if (codes.size() != 10) throw new JdbcException("注解编码位数异常");
//            if (codes.get(9) == 1) classes.add("TableId");
//            if (codes.get(8) == 1) classes.add("TableIdAuto");
//            if (codes.get(7) == 1) classes.add("TableLogic");
//            if (codes.get(6) == 1) classes.add("Version");
//            if (codes.get(5) == 1) classes.add("AllowNull");
//            if (codes.get(4) == 1) classes.add("DelKey");
//            if (codes.get(3) == 1) classes.add("EditKey");
//            if (codes.get(2) == 1) classes.add("RequiredField");
//            if (codes.get(1) == 1) classes.add("VerifyRepetitionField");
//            if (codes.get(0) == 1) classes.add("VerifyMultiRepetitionField");
//        }
//        else if (nameContainMapper) {
//            if (codes.size() != 7) throw new JdbcException("注解编码位数异常");
//            if (codes.get(6) == 1) classes.add("TableId");
//            if (codes.get(5) == 1) classes.add("TableIdAuto");
//            if (codes.get(4) == 1) classes.add("TableLogic");
//            if (codes.get(3) == 1) classes.add("Version");
//            if (codes.get(2) == 1) classes.add("AllowNull");
//            if (codes.get(1) == 1) classes.add("Primary");
//            if (codes.get(0) == 1) classes.add("Secondary");
//        }
//        else throw new JdbcException("注解类型异常");
//        return classes;
//    }

    public String convertAnnotationCode(List<String> annotations, String type) throws Exception {
        String annotationCode = null;
        if (type.equals("entity")) {
            StringBuilder code = new StringBuilder("0000000000");
            for (String annotation : annotations) {
                if (annotation.equals("TableId")) {
                    code.setCharAt(9, '1');
                    continue;
                }
                if (annotation.equals("TableIdAuto")) {
                    code.setCharAt(8, '1');
                    continue;
                }
                if (annotation.equals("TableLogic")) {
                    code.setCharAt(7, '1');
                    continue;
                }
                if (annotation.equals("Version")) {
                    code.setCharAt(6, '1');
                    continue;
                }
                if (annotation.equals("AllowNull")) {
                    code.setCharAt(5, '1');
                    continue;
                }
                if (annotation.equals("DelKey")) {
                    code.setCharAt(4, '1');
                    continue;
                }
                if (annotation.equals("EditKey")) {
                    code.setCharAt(3, '1');
                    continue;
                }
                if (annotation.equals("RequiredField")) {
                    code.setCharAt(2, '1');
                    continue;
                }
                if (annotation.equals("VerifyRepetitionField")) {
                    code.setCharAt(1, '1');
                    continue;
                }
                if (annotation.equals("VerifyMultiRepetitionField")) {
                    code.setCharAt(0, '1');
                    continue;
                }
                throw new NotExistException("注解参数不存在");
            }
            annotationCode = code.toString();
        } else if (type.equals("mapper")) {
            StringBuilder code = new StringBuilder("0000000");
            for (String annotation : annotations) {
                if (annotation.equals("TableId")) {
                    code.setCharAt(6, '1');
                    continue;
                }
                if (annotation.equals("TableIdAuto")) {
                    code.setCharAt(5, '1');
                    continue;
                }
                if (annotation.equals("TableLogic")) {
                    code.setCharAt(4, '1');
                    continue;
                }
                if (annotation.equals("Version")) {
                    code.setCharAt(3, '1');
                    continue;
                }
                if (annotation.equals("AllowNull")) {
                    code.setCharAt(2, '1');
                    continue;
                }
                if (annotation.equals("Primary")) {
                    code.setCharAt(1, '1');
                    continue;
                }
                if (annotation.equals("Secondary")) {
                    code.setCharAt(0, '1');
                    continue;
                }
                throw new NotExistException("注解参数不存在");
            }
            annotationCode = code.toString();
        } else throw new JdbcException("注解类型异常");
        return annotationCode;
    }
}
